package com.dianban.controller;

import com.dianban.base.ResultCode;
import com.dianban.base.ResultVo;
import com.dianban.entity.FacePayCommissionVerifyLog;
import com.dianban.entity.FacePayConfig;
import com.dianban.entity.User;
import com.dianban.service.FacePayCommissionVerifyLogService;
import com.dianban.service.FacePayConfigService;
import com.dianban.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Api(value = "ApiFacePayCommissionVerifyLogController", description = "佣金核销接口")
@RestController
@RequestMapping(value = "${interfacePath}/facePayCommissionVerify/")
public class ApiFacePayCommissionVerifyLogController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(ApiFacePayCommissionVerifyLogController.class);

    @Autowired
    private FacePayCommissionVerifyLogService facePayCommissionVerifyLogService;
    @Autowired
    private UserService userService;
    @Autowired
    private FacePayConfigService facePayConfigService;

    @ApiOperation(notes = "list", httpMethod = "GET", value = "佣金核销列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query",dataType = "string"),
            @ApiImplicitParam(name = "status", value = "核验状态 10 待核验；20已核验；", required = false, paramType = "query", dataType = "string"),
    })
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResultVo list(Long userId,Integer status,HttpServletRequest request, HttpServletResponse response) {
        if (userId == null) {
            return ResultVo.error(ResultCode.ERROR, "用户id不能为空");
        }
        User user = userService.findById(userId);
        if (user == null) {
            return ResultVo.error(ResultCode.ERROR, "用户id错误");
        }
        //if(status == null){
        //    status = 10;
        //}
        // 有效期改成配置
        List<FacePayConfig> facePayConfigList = facePayConfigService.find(120, 1, null);
        Integer effectiveMinute  = 30;
        if (facePayConfigList != null && !facePayConfigList.isEmpty()) {
            effectiveMinute = Integer.parseInt(facePayConfigList.get(0).getValue());
        }
        List<FacePayCommissionVerifyLog> facePayCommissionVerifyLogList =  facePayCommissionVerifyLogService.findList(status,userId,null, effectiveMinute);
        return ResultVo.success(facePayCommissionVerifyLogList, "操作成功");
    }

    @ApiOperation(notes = "verify", httpMethod = "POST", value = "佣金核销")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query",dataType = "string"),
            @ApiImplicitParam(name = "id", value = "佣金核销id", required = true, paramType = "query",dataType = "string"),
            @ApiImplicitParam(name = "code", value = "短信验证码", required = true, paramType = "query",dataType = "string"),

    })
    @RequestMapping(value = "verify", method = RequestMethod.GET)
    public ResultVo updateStatus(Long userId,Long id ,String code,HttpServletRequest request, HttpServletResponse response) {
        if (userId == null) {
            return ResultVo.error(ResultCode.ERROR, "用户id不能为空");
        }
        if (id == null) {
            return ResultVo.error(ResultCode.ERROR, "佣金核销id不能为空");
        }
        if (code == null) {
            return ResultVo.error(ResultCode.ERROR, "短信验证码不能为空");
        }
        User user = userService.findById(userId);
        if (user == null) {
            return ResultVo.error(ResultCode.ERROR, "用户id错误");
        }
        FacePayCommissionVerifyLog facePayCommissionVerifyLog = facePayCommissionVerifyLogService.findById(id);
        if (facePayCommissionVerifyLog == null) {
            return ResultVo.error(ResultCode.ERROR, "佣金核销id错误");
        }
        if(!facePayCommissionVerifyLog.getCode().equals(code)){
            return ResultVo.error(ResultCode.ERROR, "短信验证码错误");
        }
        if(facePayCommissionVerifyLog.getStatus().equals(20)){
            return ResultVo.error(ResultCode.ERROR, "佣金核销id已核销");
        }
        facePayCommissionVerifyLogService.verify(facePayCommissionVerifyLog);
        return ResultVo.success("", "操作成功");
    }

}
